package org.lites.dcimmodifier;

import android.Manifest;
import android.content.pm.PackageManager;
import android.os.Build;
import android.util.Log;
import android.widget.Toast;
import androidx.annotation.NonNull;
import androidx.core.app.ActivityCompat;
import androidx.core.content.ContextCompat;
import androidx.fragment.app.FragmentActivity;
import com.hjq.permissions.OnPermissionCallback;
import com.hjq.permissions.Permission;
import com.hjq.permissions.XXPermissions;

import java.util.ArrayList;
import java.util.List;

/**
 * 权限申请类
 */
public class PermissionsMaker {
    private FragmentActivity activity;
    private PermissionsHandler permissionsHandler;
    public PermissionsMaker(FragmentActivity activity, PermissionsHandler permissionsHandler){
        this.activity=activity;
        this.permissionsHandler =permissionsHandler;
        requestXXPermissions();
//
//        if(Build.VERSION.SDK_INT>Build.VERSION_CODES.Q) {
//            requestXXPermissions();
//
//        }else {
//
//            requestReadPermission();
//        }

    }
    private void requestXXPermissions(){

        XXPermissions.with(this.activity)
                // URL:https://github.com/getActivity/XXPermissions
                // 不适配 Android 11 可以这样写
                //.permission(Permission.Group.STORAGE)
                // 适配 Android 11 需要这样写，这里无需再写 Permission.Group.STORAGE
                .permission(Permission.MANAGE_EXTERNAL_STORAGE)
//                .permission(Permission.WRITE_EXTERNAL_STORAGE)
//                .permission(Permission.READ_EXTERNAL_STORAGE)
//                .permission(Permission.ACCESS_FINE_LOCATION)
//                .permission(Permission.ACCESS_COARSE_LOCATION)
//                .permission(Permission.ACCESS_BACKGROUND_LOCATION)
//                .permission(Permission.CAMERA)
//                .permission(Permission.RECORD_AUDIO)
                .request(new OnPermissionCallback() {

                    @Override
                    public void onGranted(List<String> permissions, boolean all) {
                        if (all) {
                            String grantstr="";
                            for (String permissionstr :
                                    permissions) {
                                grantstr=String.format("%s%s权限\r\n",grantstr,permissionstr);
                            }
                            ToastUtils.showLong(PermissionsMaker.this.activity,grantstr+"获取权限成功");
                        }
                        permissionsHandler.excute();
                    }

                    @Override
                    public void onDenied(List<String> permissions, boolean never) {
                        if (never) {
                            ToastUtils.showLong(PermissionsMaker.this.activity,"被永久拒绝授权，请手动授予权限");
                            // 如果是被永久拒绝就跳转到应用权限系统设置页面
                            XXPermissions.startPermissionActivity(PermissionsMaker.this.activity, permissions);
                        } else {
                            String denystr="";
                            for (String permissionstr :
                                    permissions) {
                                denystr=String.format("%s%s权限\r\n",denystr,permissionstr);
                            }
                            ToastUtils.showLong(PermissionsMaker.this.activity,denystr+"获取权限失败");
                        }
                        permissionsHandler.excute();
                    }
                });
    }
    // 创建一个权限列表，把需要使用而没有授权的的权限存放在这里
    private void requestReadPermission() {
        List<String> permissionList = new ArrayList<>();
        // 判断权限是否已经授予，没有就把该权限添加到列表中
//        if (ContextCompat.checkSelfPermission(this.activity, Manifest.permission.ACCESS_COARSE_LOCATION)
//                != PackageManager.PERMISSION_GRANTED) {
//            permissionList.add(Manifest.permission.ACCESS_COARSE_LOCATION);
//        }
//        if (ContextCompat.checkSelfPermission(this.activity, Manifest.permission.ACCESS_WIFI_STATE)
//                != PackageManager.PERMISSION_GRANTED) {
//            permissionList.add(Manifest.permission.ACCESS_WIFI_STATE);
//        }
//        if (ContextCompat.checkSelfPermission(this.activity, Manifest.permission.CHANGE_WIFI_STATE)
//                != PackageManager.PERMISSION_GRANTED) {
//            permissionList.add(Manifest.permission.CHANGE_WIFI_STATE);
//        }
//        if (ContextCompat.checkSelfPermission(this.activity, Manifest.permission.CHANGE_NETWORK_STATE)
//                != PackageManager.PERMISSION_GRANTED) {
//            permissionList.add(Manifest.permission.CHANGE_NETWORK_STATE);
//        }
//        if (ContextCompat.checkSelfPermission(this.activity, Manifest.permission.ACCESS_NETWORK_STATE)
//                != PackageManager.PERMISSION_GRANTED) {
//            permissionList.add(Manifest.permission.ACCESS_NETWORK_STATE);
//        }
//        if (ContextCompat.checkSelfPermission(this.activity, Manifest.permission.BLUETOOTH_ADMIN)
//                != PackageManager.PERMISSION_GRANTED) {
//            permissionList.add(Manifest.permission.BLUETOOTH_ADMIN);
//        }
//        if (ContextCompat.checkSelfPermission(this.activity, Manifest.permission.READ_PHONE_STATE)
//                != PackageManager.PERMISSION_GRANTED) {
//            permissionList.add(Manifest.permission.READ_PHONE_STATE);
//        }
//        if (ContextCompat.checkSelfPermission(this.activity, Manifest.permission.ACCESS_FINE_LOCATION)
//                != PackageManager.PERMISSION_GRANTED) {
//            permissionList.add(Manifest.permission.ACCESS_FINE_LOCATION);
//        }
//        if (ContextCompat.checkSelfPermission(this.activity, Manifest.permission.INTERNET)
//                != PackageManager.PERMISSION_GRANTED) {
//            permissionList.add(Manifest.permission.INTERNET);
//        }


        if(Build.VERSION.SDK_INT>Build.VERSION_CODES.Q) {
            if (ContextCompat.checkSelfPermission(this.activity, Manifest.permission.MANAGE_EXTERNAL_STORAGE)
                    != PackageManager.PERMISSION_GRANTED) {
                permissionList.add(Manifest.permission.MANAGE_EXTERNAL_STORAGE);
            }
        }else {
            if (ContextCompat.checkSelfPermission(this.activity, Manifest.permission.WRITE_EXTERNAL_STORAGE)
                    != PackageManager.PERMISSION_GRANTED) {
                permissionList.add(Manifest.permission.WRITE_EXTERNAL_STORAGE);
            }

            if (ContextCompat.checkSelfPermission(this.activity, Manifest.permission.READ_EXTERNAL_STORAGE)
                    != PackageManager.PERMISSION_GRANTED) {
                permissionList.add(Manifest.permission.READ_EXTERNAL_STORAGE);
            }
        }
//        if (ContextCompat.checkSelfPermission(this.activity, Manifest.permission.BLUETOOTH)
//                != PackageManager.PERMISSION_GRANTED) {
//            permissionList.add(Manifest.permission.BLUETOOTH);
//        }
        //******安卓10.0：添加进程获取位置信息权限***********************
        /*if (ContextCompat.checkSelfPermission(this, Manifest.permission.ACCESS_BACKGROUND_LOCATION)
                != PackageManager.PERMISSION_GRANTED) {
            permissionList.add(Manifest.permission.ACCESS_BACKGROUND_LOCATION);
        }*/
//        if (ContextCompat.checkSelfPermission(this.activity,Manifest.permission.CAMERA)!= PackageManager.PERMISSION_GRANTED)
//        {
//            permissionList.add(Manifest.permission.CAMERA);
//        }
//        if (ContextCompat.checkSelfPermission(this.activity,Manifest.permission.RECORD_AUDIO)!= PackageManager.PERMISSION_GRANTED)
//        {
//            permissionList.add(Manifest.permission.RECORD_AUDIO);
//        }
        //**************************************************************
        // 如果列表为空，就是全部权限都获取了，不用再次获取了。不为空就去申请权限
        if (!permissionList.isEmpty()) {
            ActivityCompat.requestPermissions(this.activity,
                    permissionList.toArray(new String[0]), 1002);
        }
        else{
            ToastUtils.showLong(PermissionsMaker.this.activity, "多个权限你都有了，不用再次申请");
            permissionsHandler.excute();
        }
    }

//    public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
//
//        boolean hasPermissionDismiss = false;//有权限没有通过
//        if (requestCode == 1002) {// 1002请求码对应的是申请多个权限
//            if (grantResults.length > 0) {
//                // 因为是多个权限，所以需要一个循环获取每个权限的获取情况
//                for (int i = 0; i < grantResults.length; i++) {
//                    // PERMISSION_DENIED 这个值代表是没有授权，我们可以把被拒绝授权的权限显示出来
//                    if (grantResults[i] == PackageManager.PERMISSION_DENIED) {
//                        hasPermissionDismiss = true;
//                        ToastUtils.showLong(this.activity, permissions[i] + "权限被拒绝了");
//                        break;
//                    }
//                }
//            }
//        }
//        if (hasPermissionDismiss) {
////            if(grantResults.length==1&&permissions[0].equals(Manifest.permission.ACCESS_BACKGROUND_LOCATION) ){
////                authed_execute();
////            }
////            else{
////                requestReadPermission();
////            }
//            ToastUtils.showLong(this.activity, "存在权限被拒绝，程序可能无法执行");
//        }
////        else {
//        ToastUtils.showLong(this.activity, "执行后续程序");
//        permissionsHandler.excute();
////        }
//    }

    abstract public static class PermissionsHandler {
        public abstract void excute();
    }

}
